package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * 普通用户注册
 */
public class UserRegister extends Utils {

    private static String url = "http://82.156.185.199:8080/admin.html";

    public UserRegister() {
        super(url);
    }

    /**
     * 页面元素
     */
    public void testPage(){
        driver.findElement(By.cssSelector("#register")).click();
        driver.switchTo().frame(0);
        // "填写注册信息"
        driver.findElement(By.cssSelector("body > div > h2"));
        // "姓名"
        driver.findElement(By.cssSelector("#registerForm > div:nth-child(1) > label"));
        // "姓名" 输入框
        driver.findElement(By.cssSelector("#name"));
        // "邮箱"
        driver.findElement(By.cssSelector("#registerForm > div:nth-child(2) > label"));
        // "邮箱" 输入框
        driver.findElement(By.cssSelector("#mail"));
        // "手机号"
        driver.findElement(By.cssSelector("#registerForm > div:nth-child(3) > label"));
        // "手机号" 输入框
        driver.findElement(By.cssSelector("#phoneNumber"));
        // "注册" 按钮
        driver.findElement(By.cssSelector("#registerForm > button"));
    }

    /**
     * 用户注册失败
     */
    public void userRigisterFail() throws InterruptedException {
        // 注册失败
        // 什么都不填写
        driver.switchTo().defaultContent();
        driver.findElement(By.cssSelector("#register")).click();
        driver.switchTo().frame(0);
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        // 提示
        // "请输入您的姓名"
        driver.findElement(By.cssSelector("#name-error"));
        // "请输入有效的邮箱地址"
        driver.findElement(By.cssSelector("#mail-error"));
        // "请输入您的手机号"
        driver.findElement(By.cssSelector("#phoneNumber-error"));

        // 输入错误格式的手机号
        driver.findElement(By.cssSelector("#name")).sendKeys("张七");
        driver.findElement(By.cssSelector("#mail")).sendKeys("145@qq.com");
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("1451111111");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
//        driver.switchTo().defaultContent();
        String expectText = "注册失败！undefined";
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert1 = driver.switchTo().alert();
        String actualText1 = alert1.getText();
        Assertions.assertEquals(expectText, actualText1);
        alert1.accept();

        // 输入注册过的手机号
//        driver.switchTo().frame(0);
        driver.findElement(By.cssSelector("#phoneNumber")).clear();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15714393906");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert2 = driver.switchTo().alert();
        String actualText2 = alert2.getText();
        Assertions.assertEquals(expectText, actualText2);
        alert2.accept();
    }

    /**
     * 用户注册成功
     */
    public void userRegisterSuc(){
        // 清空输入框

        // 注册成功
        driver.findElement(By.cssSelector("#name")).sendKeys("张十");
        driver.findElement(By.cssSelector("#mail")).sendKeys("148@qq.com");
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("14811111111");
        driver.findElement(By.cssSelector("#registerForm > button")).click();
    }
}
